﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SystemDoc_codeRule_tree.aspx.cs" Inherits="Ccit.OA.SystemDoc.SystemDoc_codeRule_tree" %>
<%@ Register assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="Microsoft.Web.UI.WebControls" tagprefix="iewc" %>
<%@ Register TagPrefix="ccit" Namespace="Ccit.ServerControls" Assembly="Ccit.ServerControls.ActionForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>编号规则设置树页面</title>
        <script language="javascript" type="text/javascript" src="../script/jquery.js"></script>
        <script language="javascript" type="text/javascript" src="../script/StringClass_Ext.js"></script>
        <script language="javascript" type="text/javascript" src="../script/jsonlib.js"></script>
        <script language="javascript" src="../script/jwindow.js" type="text/javascript"></script>
        <script language="javascript" src="../script/jcontrol.js" type="text/javascript"></script>
        <script language="javascript" src="../script/jbase64.js" type="text/javascript"></script>
</head>
<body onclick="hideMenu()" >
    <ccit:ActionForm id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>    
    <input type="hidden" id="_hidClassType" runat="server" />
    <input type="hidden" id="_hidOriUpAddrId" runat="server" />
    <input type="hidden" id="_hidUpAddrId" runat="server" />  
    <input type="hidden" id="_hidAddNewSign" runat="server" /> <!-- 新增记录标志 -->
    <input type="hidden" id="_hidDelDepAddressId" runat="server" />  
    
        <!--保存后传来的主键id-->
    <input type="hidden" id="_hidNewColumnId" runat="server" />
    <input type="hidden" id="_hidExpandSign" runat="server" />
    <input type="hidden" id="_hidLevelStruct" runat="server" /> 
   
    <iewc:TreeView ID="trvDepAddress" runat="server">
    </iewc:TreeView>
    <br />
    <!--编辑页面保存成功的时候  调用的事件-->
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display:none" value="触发事件" />
    <!--编辑页面删除成功的时候  调用的事件-->
    <input type="button" id="btnDelTrigger" onclick="doDelTrigger()" style="display:none" value="删除触发事件" />
    <script type="text/javascript">
    <!--
        var menuskin = "skin";
		var node = null;
		var menuname = "root";	 
		 
		function hideMenu() 
		{		    
			var obj = document.getElementById(menuname);
			if (null != obj && typeof(obj) != "undefined")
				document.getElementById(menuname).style.visibility = "hidden";
		}
		
		function highlighItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "highlight";
				event.srcElement.style.color = "white";
			}
		}
		function lowlightItem() 
		{
			if (event.srcElement.className == "menuitems") 
			{
				event.srcElement.style.backgroundColor = "";
				event.srcElement.style.color = "black";
				window.status = "";
			}
		}
		 
		///节点展开
		function trvDepAddress.onexpand()
		{	
		    /* Step 1：find current click TreeNode */	    
		    var nodeIndex = event.treeNodeIndex;	
		    //alert(nodeIndex);		
			if (typeof(nodeIndex) == "undefined"){			
				node = null;
				return false;
			}
			
			trvDepAddress.selectedNodeIndex = nodeIndex;
			node = trvDepAddress.getTreeNode(nodeIndex);
						
			/* Step 2: get asset type id，and get all child types */			
			var vNodeId = node.getAttribute("ID");
			var vNodeIdArray = vNodeId.split("_")
			var vAddressId = vNodeIdArray[0];
			var vLoadSign = vNodeIdArray[1];		
			
			if(vAddressId != "0" && vLoadSign == "0") {
			    var oSubNodes = node.getChildren();
			    var oSubNode;
			    for(var i = oSubNodes.length - 1; i >= 0; i --) {
			        oSubNode = oSubNodes[i];
			        oSubNode.remove();
			    }
			     var expandSign = $get("_hidExpandSign").value;//是否逐级展开 1：是
			    var levelStruct = $get("_hidLevelStruct").value;			    
			    PageMethods.GetSubAddrsByUpAddrId(vAddressId,nodeIndex,expandSign,levelStruct,ClientBack);
			    
			}
			hideMenu();
		}
		///节点收缩
		function trvDepAddress.oncollapse()
		{
			hideMenu();
		}
		 
		function ClientBack(vResult)
		{
		     var vNodeIdArray = vResult.split("_") 
		     var vOperType=vNodeIdArray[0];
		     switch(vOperType) {		       
                case "GetSubTypesByColumnId"://获取子节点
                     var vExeResult = vNodeIdArray[1];
	                 var nodeIndex = vNodeIdArray[2];
	                 var expandSign = vNodeIdArray[3];
	                 var levelStruct = vNodeIdArray[4];
	                 var parentNodeId = vNodeIdArray[5];
    			 
                    eval("var objResult="+vExeResult); 
                    for(var i=0;i<objResult.length;i++)
                    {
                       var oResult=objResult[i];
                       createSubTNodes(nodeIndex,oResult);//展开老祖宗
                    }         
                
                if(expandSign == "1") {//是逐级展开
                    var parentIdArr = levelStruct.split(",");
                    var iLen = parentIdArr.length;
                    var bGet = false;
                    for(var i = 0; i < iLen; i ++) {                                    
                        var colId = parentIdArr[i];     
                        if(!bGet) {
                            if(colId == parentNodeId) {
                                if(i == (iLen - 1)) {//如果到最底层，则逐级展开标志0
                                    $get("_hidExpandSign").value = "0";                                
                                }
                                bGet = true;
                                continue;
                            }
                        }
                        else {//true，展开下级                             
                            var ctrlNode = findTreeNode(colId);
                            var nodeAttrId = ctrlNode.getAttribute("ID");    
                            var vNodeIdArray = nodeAttrId.split("_")
	                        if(vNodeIdArray[1] == "1") {
	                            if(i == (iLen - 1)) {
                                    ctrlNode.setAttribute("EXPANDED",true);                               
                                }
                                else
	                                continue;
	                        }
	                        else {
	                            ctrlNode.setAttribute("EXPANDED",true);
	                            break;
	                        }
                        }
                    } 
                 }
                   break;
//                 case "GetNodeLevelStruct":
//                     var vResult=vNodeIdArray[1];
//                     eval("var objResult="+vResult); 
//		             var levelStruct = objResult.resultIds;
//		             $get("_hidLevelStruct").value = levelStruct;
//                     
//                     var parentIdArr = levelStruct.split(",");
//                     var iLen = parentIdArr.length;
//                     var colId = "";
//                     var ctrlNode;
//                     var nodeAttrId = "";
//                     for(var i = 0; i < iLen; i ++) {
//                        colId = parentIdArr[i];
//                        ctrlNode = findTreeNode(colId);
//                        nodeAttrId = ctrlNode.getAttribute("ID");
//                                        
//		                var vNodeIdArray = nodeAttrId.split("_")
//			            if(vNodeIdArray[1] == "1") { //判断最一层节点是否展开过 1--：展开过
//			                continue; 
//			            }
//			            else {
//			                ctrlNode.setAttribute("EXPANDED",true);//没展开连续展开调trvDepAddress.onexpand()方法展开最老祖宗
//			                break;
//			            }
//                     } 
//                         objResult = null;     
//                         break; 
                }
       		}
		 
        //追加子节点
        function createSubTNodes(nodeIndex,oResult) { 
			     var node = $get("trvDepAddress").getTreeNode(nodeIndex); 
			     var sClassType = $get("_hidClassType").value;
			     var vPageParam = "CodeID=" + oResult.CodeId+"&classType="+sClassType+"";
                 var encodeParam = base64encode(vPageParam);
                 vNavigateUrl = "SystemDoc_codeRule_edit.aspx?" + encodeParam;
                 oSubNode = $get("trvDepAddress").createTreeNode();
		         oSubNode.setAttribute("ID",oResult.CodeId + "_" +"0");
		         oSubNode.setAttribute("Text",oResult.CodeName);
		         oSubNode.setAttribute("Type","root");
		         oSubNode.setAttribute("Target","iFrmEdit");
		         oSubNode.setAttribute("ImageUrl","../images/toolbar/filelogo.gif");
		         oSubNode.setAttribute("NavigateUrl",vNavigateUrl); 
		          
		         node.add(oSubNode);
		        /* set node load sign = 1 */
		        var vNodeId = node.getAttribute("ID");
		        var vNodeIdArray = vNodeId.split("_")
			    oResult.CodeId = vNodeIdArray[0]; 
			    node.setAttribute("ID",oResult.CodeId  + "_"+"1");		    
        }
        ///查找节点
        function findTreeNode(findAstTypeId) {
            var ctrlTree = document.getElementById("trvDepAddress");		    
		    
		    var ctrlRoot = ctrlTree.getTreeNode("0"); 
		    var ctrlChildNodes = ctrlRoot.getChildren();
	    
		    var ctrlFindNode = recursionFind(ctrlChildNodes,findAstTypeId);
            return ctrlFindNode;

        }
        
        function recursionFind(ctrlChildNodes,findAddrId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
		    var vNodeId;
		    var vNodeIdArray;
		    var vAddressId;
		    var ctrlSubChildNodes;
		    var bFind = false;
		    
            for(var i = 0; i < iLen; i ++) {
		        ctrlTreeNode = ctrlChildNodes[i];
		        vNodeId = ctrlTreeNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_")
		        vAddressId = vNodeIdArray[0];
		        if(vAddressId == findAddrId) {		           
		            bFind = true;
		            break;       
		        }
		        else {
		            ctrlSubChildNodes = ctrlTreeNode.getChildren();
		            ctrlTreeNode = recursionFind(ctrlSubChildNodes,findAddrId);
		            if(null != ctrlTreeNode) {
		                bFind = true;
		                break;
		            }
		                
		        }
		    }
            
            if(bFind)
                return ctrlTreeNode;
            else
                return null;
        }
        //编辑页面保存成功的时候  调用的方法
        function doTrigger() {		    
		     doOperOriParentNode();//找原父节点
             doOperDtParentNode();//找新父节点
		}
		//找原父节点
		function doOperOriParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpAddrId").value; //原父节点的主键(绑定到树节点的主键)
		    var vUpTypeId = $get("_hidUpAddrId").value;		 //新父节点的主键(绑定到树节点的主键)
		    var isAddNew = $get("_hidAddNewSign").value;   //是否为编辑 1:编辑
		    
		        
		    if(isAddNew=="1"){//编辑
		        if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
		            var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		            eval("var curNodeObj=" + curNodeExpr);		           
		            var colName = curNodeObj.CodeName;
		            var ctrClickObj = findTreeNode(curNodeObj.CodeId);		
		            ctrClickObj.setAttribute("Text",colName);
		            return;
		        }
		        /* 初始化全局变量 */
		        ctrlOgNode = null;		    
		        vOgNodeIndex = "";
		        // get root node object
		        var ctrlTree = document.getElementById("trvDepAddress");	
		        var ctrlRoot = ctrlTree.getTreeNode("0");
    		    
		        /* 获取原始上级节点对象 */
		        //根据原父节点主键获取其树的节点索引(eg：根据点击树主键3，获取其树的索引0.1)
		        if(vOriUpTypeId != ""&&vOriUpTypeId!="0") {
		            ctrlOgNode = findTreeNode(vOriUpTypeId);  //根据原父节点主键获取其树的节点对象 
		            vOgNodeIndex = ctrlOgNode.getNodeIndex();  //根据节点对象获取树节点索引eg:0.0.1
		        }
		        else {   //无上级父节点时
		            ctrlOgNode=ctrlRoot;  
		            vOgNodeIndex= "0";
		        }
		        var curNodeExpr = $get("_hidNewColumnId").value;   //获取的是当前编辑的节点对象 eg:{"columnId":"26","columnName":"测试类型","hasChild":"1"}
		        eval("var curNodeObj=" + curNodeExpr);
                // find current node position index
                var curNode = findTreeNode(curNodeObj.CodeId);  //获取当前编辑节点对象对应的树节点对象
                
                // delete origin node
                curNode.remove();  //移除
                
                curNodeObj = null;
		    }
		    return;		    
		}
		//找新父节点
		function doOperDtParentNode() {
		    var vOriUpTypeId = $get("_hidOriUpAddrId").value;  
		    var vUpTypeId = $get("_hidUpAddrId").value;		//新的上级节点    
		    var isAddNew = $get("_hidAddNewSign").value; //isAddNew = 1 时为编辑
		    
		    /* 初始化全局变量 */
		    ctrlDtNode = null;		    
		    vDtNodeIndex = "";
		    // get root node object
		    var ctrlTree = document.getElementById("trvDepAddress");	
		    var ctrlRoot = ctrlTree.getTreeNode("0");
		    /* 获取新上级节点对象 */
		    if(vUpTypeId != ""&& vUpTypeId!="0") {		        
		        ctrlDtNode = findTreeNode(vUpTypeId);	  //新上级节点对象	
		        if(null == ctrlDtNode) {    //找不到新父节点
		            expandDtNode(vUpTypeId);
		            return;
		        }
		        vDtNodeIndex = ctrlDtNode.getNodeIndex();	
		        var ctrlSubChildNodes = ctrlDtNode.getChildren();
		        var iChildLength = ctrlSubChildNodes.length;
		        
		        var bHasChild = false;
		        if(iChildLength > 0)
		           bHasChild = true;		    
		           
		        var ArrayID = ctrlDtNode.getAttribute("ID").split("_");
		        if(bHasChild == true){
		           if(ArrayID[1]=="0"){ //ArrayID[1]=="0"未展开 ;bHasChild != null 有子节点
		             ctrlDtNode.setAttribute("EXPANDED",true);
		             return ;
		          }
		       } 
		    }
		    else {	
		        ctrlDtNode=ctrlRoot;	
		        vDtNodeIndex= "0";  
		    } 
		    var curNodeExpr = $get("_hidNewColumnId").value;
		    eval("var curNodeObj=" + curNodeExpr);
		    
		    if(isAddNew=="1"){//编辑
		        if(vUpTypeId == vOriUpTypeId){//原节点与新节点相等
		            return;
		        }
		        else {		           
		            // add node to destination parent node	
		              createSubTNodes(vDtNodeIndex,curNodeObj);
		        }   
		    }
		    else //新增
		    {		       	                
		       createSubTNodes(vDtNodeIndex,curNodeObj);
		    } 
		    ctrlDtNode.setAttribute("EXPANDED",true);
		    curNodeObj = null;
		}
		
		function expandDtNode(dtNodeId) {
		    $get("_hidExpandSign").value = "1";//连续展开标志
		    //PageMethods.GetNodeLevelStruct(dtNodeId,ClientBack);  //获取新父节点上级节点
		} 
		
		//编辑页面删除成功的时候  调用的方法
		function doDelTrigger() {		     
		    var delAddrId = $get("_hidDelDepAddressId").value;
		    ctrlNode = findTreeNode(delAddrId);
		    nodeIndex = ctrlNode.getNodeIndex();
		    var node = $get("trvDepAddress").getTreeNode(nodeIndex); 
            node.remove();            
		}
		
		function initialTreeNode(ctrlFindNode) {
		    var ctrlTree = document.getElementById("trvDepAddress");	
		    var vNodeId;
		    var vNodeIdArray;
		    var vAddrId;	
		    var vNodeIndex; 
		    if(null != ctrlFindNode) {
		        vNodeId = ctrlFindNode.getAttribute("ID");    
		        vNodeIdArray = vNodeId.split("_");
		        vAddrId = vNodeIdArray[0]; 
		        ctrlFindNode.setAttribute("ID",vAddrId + "_" +"0");
		        vNodeIndex = ctrlFindNode.getNodeIndex();
		        ctrlTree.selectedNodeIndex = vNodeIndex;
		        /* 如果当前节点没有下级节点，则不会触发expand事件,
		         * 此时需要在当前节点下增加一个空节点 */
		        var vChildNodes = ctrlFindNode.getChildren();
		        if(vChildNodes.length == 0) {
		           var oEmptyNode = $get("trvDepAddress").createTreeNode();  
		           ctrlFindNode.add(oEmptyNode);
		        }
		        ctrlFindNode.setAttribute("EXPANDED",true);
		    }
		}
    //-->
    </script>
    
   </ccit:ActionForm>
</body>
</html>
